package com.macrohard.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.macrohard.mapper.domain.Activity;
import com.macrohard.admin.service.ActivityService;
import com.macrohard.mapper.domain.Address;
import com.macrohard.mapper.domain.Teacher;
import com.macrohard.mapper.mapper.ActivityMapper;
import com.macrohard.mapper.mapper.AddressMapper;
import com.macrohard.mapper.mapper.TeacherMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 27371
* @description 针对表【activity】的数据库操作Service实现
* @createDate 2022-07-14 12:32:00
*/
@Service
@RequiredArgsConstructor
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity>
    implements ActivityService{

	private final ActivityMapper activityMapper;


	private final TeacherMapper teacherMapper;


	private final AddressMapper addressMapper;


	//@CacheResult(cacheName = "activity-list")
	public Page<Activity> listAll(Page<Activity> page, Wrapper<Activity> wrapper) {
		Page<Activity> selectPage = activityMapper.selectPage(page, wrapper);
		List<Activity> resList = selectPage.getRecords().stream().map(this::combineActivityWithTeacherAndAddress).collect(Collectors.toList());
		selectPage.setRecords(resList);
		return selectPage;
	}

	public Activity getOne(int id) {
		Activity activity = activityMapper.selectById(id);
		return combineActivityWithTeacherAndAddress(activity);
	}

	private Activity combineActivityWithTeacherAndAddress(Activity activity) {
		String teacherId = activity.getrTId();
		Integer addressId = activity.getrAddress();
		LambdaQueryWrapper<Teacher> teacherWrapper = new LambdaQueryWrapper<>();
		teacherWrapper.eq(Teacher::gettTeacherid, teacherId);
		Teacher teacher = teacherMapper.selectOne(teacherWrapper);
		Address address = addressMapper.selectById(addressId);
		activity.setrT(teacher);
		activity.setrAddressBody(address);
		return activity;
	}
}




